package com.java.struts.menu.dao;

import com.java.struts.base.util.HibernateUtil;
import com.java.struts.menu.entity.MenuinfoEntity;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

public class MenuinfoDao {
    /**
     * 查询所有菜品
     * @return
     */
    public MenuinfoEntity queryMenuMname(Integer meid){
        //执行sql语句
        Query query=HibernateUtil.getQuery("from MenuinfoEntity where meid=?1");
        query.setParameter(1,meid);
        //拿到数据集
        MenuinfoEntity menuinfoEntity= (MenuinfoEntity) query.uniqueResult();
        //返回数据集
        return  menuinfoEntity;
    }

    /**
     * 查询所有菜品
     * @return list
     */
    public List<MenuinfoEntity> listMenu(){
        Query query=HibernateUtil.getQuery("from MenuinfoEntity");
        List<MenuinfoEntity> list=query.list();
        return  list;
    }

    /**
     *添加菜品
     * @param menuinfo
     */
    public void addMenu(MenuinfoEntity menuinfo){
        Session session  = HibernateUtil.getSession();
        Transaction transaction=session.beginTransaction();
        session.save(menuinfo);
        transaction.commit();
        session.close();
    }

    /**
     * 删除菜品
     * @param menuId
     */
    public void deleteMenu(Integer menuId){
        Session session  = HibernateUtil.getSession();
        Transaction transaction=session.beginTransaction();
        MenuinfoEntity menuinfo = new MenuinfoEntity();
        menuinfo.setMeid(menuId);
        session.delete(menuinfo);
        transaction.commit();
        session.close();
    }


    /**
     *  修改菜品
     * @param menuinfo
     */
    public void updateMenu(MenuinfoEntity menuinfo){
        //  获取session对象
        Session session  = HibernateUtil.getSession();

        session.update(menuinfo);

        session.close();
    }
}
